class_name HoldPoisonBow
extends BaseHoldItem

@onready var muzzle: Marker2D = $Muzzle


func _ready() -> void:
	set_show_ui_bullet_info()
	

func update_bullet_count():
	var player: Player = holder as Player
	var config = item_data.item_config as ItemBowConfig
	var bullet_count = player.player_data.get_bullet_count(config.bullet_type)
	GlobalSignals.bullet_count_changed.emit(bullet_count)
	

func set_show_ui_bullet_info():
	var player: Player = holder as Player
	player.level.ui_bullet_remain_info.visible = true
	update_bullet_count()
	

func set_hide_ui_bullet_info():
	var player: Player = holder as Player
	player.level.ui_bullet_remain_info.visible = false
	

func shoot():
	var player: Player = holder as Player
	var bow_config = item_data.item_config as ItemBowConfig
	var remain_bullet_count = player.player_data.get_bullet_count(bow_config.bullet_type)
	if remain_bullet_count <= 0: return
	
	player.player_data.reduce_bullet(bow_config.bullet_type)
	update_bullet_count()
	
	var inst = (ResourceManager.get_resource("bullet_poison_arrow") as PackedScene).instantiate() as BulletPoisonArrow
	var level = get_tree().current_scene as Level
	level.bullets_parent.add_child(inst)
	
	inst.sender = holder
	inst.global_position = muzzle.global_position
	inst.rotation = muzzle.global_rotation
	inst.attack = bow_config.attack + player.player_data.get_attack()
	inst.critical_rate = bow_config.critical_rate
	inst.critical_bonus = bow_config.critical_bonus
	inst.knockback = bow_config.knockback
	inst.poison_hit_rate = bow_config.poison_hit_rate
	inst.poison_damage_per_second = bow_config.poison_damage_per_second
	inst.poison_debuff_duration = bow_config.poison_debuff_duration
	
	var shoot_direction = Vector2.from_angle(player.gun_hand.global_rotation)
	inst.set_start_velocity(shoot_direction * bow_config.shoot_start_speed)
